package com.example.wangfanwangluo.wangfanboss;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DocumentDetail extends AppCompatActivity implements View.OnClickListener  {
    private TextView orderinfo_back;  //返回按钮
    private RelativeLayout orderinfo_detail;  //
    private RelativeLayout orderinfo_track;
    private Fragment orderinfoFm; //运单详情fragment
    private Fragment trackFm;  //运单跟踪fragment
    private TextView orderinfo_oiline;  //运单详情下的绿色线条
    private TextView orderinfo_trackline; //运单跟踪下的绿色线条
    private TextView orderinfo_title; //详情界面的title

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document_detail);
        //隐藏标题栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        }
        initView();
        initEvents();

        Bundle bundle = getIntent().getExtras();
        //根据type值判断进入运单详情界面/跟踪界面
        String type = bundle.getString("type");
        if (type.equals("detail")) {
            select(0); //详情界面
        } else if (type.equals("track")) {
            select(1); //跟踪界面
        }
    }

    private void initView(){
        orderinfo_back = (TextView) findViewById(R.id.orderinfo_back);
        orderinfo_detail = (RelativeLayout) findViewById(R.id.orderinfo_detail);
        orderinfo_track = (RelativeLayout) findViewById(R.id.orderinfo_track);
        orderinfo_oiline = (TextView) findViewById(R.id.orderinfo_oiline);
        orderinfo_trackline = (TextView) findViewById(R.id.orderinfo_trackline);
        orderinfo_title = (TextView) findViewById(R.id.orderinfo_title);
    }

    private void initEvents(){
        orderinfo_detail.setOnClickListener(this);
        orderinfo_track.setOnClickListener(this);
        orderinfo_back.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.orderinfo_detail://选择运单详情
                select(0); //切换fragment
                break;
            case R.id.orderinfo_track://选择运单跟踪
                select(1);
                break;
            case R.id.orderinfo_back://返回列表页
                finish();
                break;
            default:
                break;
        }
    }
    //隐藏fragment
    private void hideFragment(FragmentTransaction transaction){
        if (orderinfoFm != null){
            //如果当前有运单详情fragment时，隐藏
            transaction.hide(orderinfoFm);
        }

        if (trackFm != null){
            //如果当前有运单跟踪fragment时，隐藏
            transaction.hide(trackFm);
        }
        orderinfo_oiline.setVisibility(View.GONE);
        orderinfo_trackline.setVisibility(View.GONE);
    }

    private void select(int i){
        //获取fragmentManger
        FragmentManager fragmentManager = getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if (orderinfoFm == null){
                    //如果当前没有运单详情fragmen，则创建fragment
                    orderinfoFm = new FragmentDocuDetail();
                    //将运单详情fragment添加到fragmentTransaction事务中
                    fragmentTransaction.add(R.id.orderinfo_fragment, orderinfoFm);
                } else {
                    fragmentTransaction.show(orderinfoFm);
                }
                orderinfo_oiline.setVisibility(View.VISIBLE);
                orderinfo_trackline.setVisibility(View.GONE);
                orderinfo_title.setText("运单详情");
                break;
            case 1:
                if (trackFm == null){
                    trackFm = new FragmentGenzongDetail();
                    fragmentTransaction.add(R.id.orderinfo_fragment, trackFm);
                } else {
                    fragmentTransaction.show(trackFm);
                }
                orderinfo_oiline.setVisibility(View.GONE);
                orderinfo_trackline.setVisibility(View.VISIBLE);
                orderinfo_title.setText("运单跟踪");
                break;
            default:
                break;
        }
        //提交事务，用commit来完成
        fragmentTransaction.commit();
    }
}